package com.runa.rccprotocol.analysis.zhongrun;

import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.equdata.HeatMeterData;
import com.runa.rccprotocol.dto.equdata.TerminalCalendarClock;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author diandian
 * @Date 2022/4/26 14:29
 */
public class ZhongRunMeterDataAnalysis extends MetaBufferAnalysis<byte[]> {

    private DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");


    @Override
    public int fill(byte[] data, int length,String netEquNo) {
        int begin = -1;
        for (int i = 0; i < length; i++) {
            if (0x68 == (data[i] & 0xff)) {
                begin = i;
                break;
            }
        }
        if (begin == -1) {
            return 0;
        }

        int index = begin;
        //表设备
        ++index;
        boolean meterEqu = (data[index] & 0xff) == 0x20;
        //设备编号
        ++index;
        String equSerialNo = ByteUtil.byteToHexStringFlip(data, index, 4);
        index += 4;
        //厂商代码  其中 A5(59),A6(42)为厂商代码
        String factoryCode = ByteUtil.byteToHexString(data, index, 3);
        //中润厂家 001111
        if (!"001111".equals(factoryCode)) {
            return length - index;
        }

        index += 3;
        //控制码
        int ctr = data[index];
        ++index;
        //数据长度
        int dataLen = data[index] & 0xff;
        //判断报文是否完整，已16结尾
        byte end = data[index + dataLen + 2];
        if (0x16 != (end & 0xff)) {
            return length - end;
        }
        ++index;
        //校验判断
        byte sum = ByteUtil.sum(data, begin, begin + dataLen + 11);
        if ((sum & 0xff) != data[index + dataLen]) {
            return length - (begin + dataLen + 13);
        }

        //数据标识 901F
        if (!"1F90".equals(ByteUtil.byteToHexString(data, index, 2))) {
            return length - (begin + dataLen + 13);
        }
        index += 2;
        //序列号
        byte s = data[index];
        ++index;

        int result = 0;
        if (meterEqu) {
            HotMeterVO meterData = new HotMeterVO();
            meterData.setMeterSerial(equSerialNo);
            result = handleMeterData(data, index, length,meterData);
        }

        return result;
    }


    private int handleMeterData(byte[] data, int index, int length, HotMeterVO meterData) {
        //冷量
        index += 5;

        //累热
        meterData.setAccHeat((float) (ByteUtil.bcdToInt(data, index, 4, false) * 0.01));
        index += 4;
        //跳过单位
        ++index;

        //热功率
        meterData.setPower((float) (ByteUtil.bcdToInt(data, index, 4, false) * 0.01));
        index += 4;
        //跳过单位
        ++index;

        //瞬流
        meterData.setFlow((float) (ByteUtil.bcdToInt(data, index, 4, false) * 0.01));
        index += 4;
        //跳过单位
        ++index;

        //累流
        meterData.setAccFlow((float) (ByteUtil.bcdToInt(data, index, 4, false) * 0.01));
        index += 4;
        //跳过单位
        ++index;

        //供水温度
        meterData.setSupplyTemp((float) (ByteUtil.bcdToInt(data, index, 3, false) * 0.01));
        index += 3;

        //回水温度
        meterData.setReturnTemp((float) (ByteUtil.bcdToInt(data, index, 3, false) * 0.01));
        index += 3;

        //累计工作时间
        int workTime = ByteUtil.bcdToInt(data, index, 3, false);
        meterData.setWorkTime(workTime);
        index += 3;

        //设备时间
        String equTime = ByteUtil.byteToHexStringFlip(data, index, 7);
        try {
            Date parse = format.parse(equTime);
            meterData.setMeterDate(parse);
        } catch (ParseException e) {
            
        }
        index += 7;

        //状态
        index += 2;

        RunaConcenResponseData responseData = new RunaConcenResponseData();
        responseData.addRepData(meterData);
        meterData.setReadDate(new Date());
        this.responseData = responseData;
        return length - index - 2;
    }

    public static void main(String[] args) {
        String s = "68 \n" +
                "20 \n" +
                "58 08 08 19 \n" +
                "00 11 11 \n" +
                "81 \n" +
                "2E \n" +
                "1F 90 \n" +
                "12 \n" +
                "00 00 00 00 05 \n" +
                "00 00 24 01 05 \n" +
                "00 00 00 00 17 \n" +
                "00 00 00 00 35 \n" +
                "75 49 10 00 2C \n" +
                "88 19 00 \n" +
                "70 19 00 \n" +
                "02 15 02 \n" +
                "13 29 15 26 04 22 20 \n" +
                "00 00 \n" +
                "10 16";

        byte[] data = ByteUtil.hexStringToByte(s.replace(" ", "").replace("\n",""));
        ZhongRunMeterDataAnalysis analysis = new ZhongRunMeterDataAnalysis();
        analysis.fill(data, data.length,"");

    }


}
